package com.yls.except.util.img;//导入需要的包
import java.io.File;
import java.io.FileOutputStream;  
import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.image.BufferedImage;  
  
/* 
 * 当导入一下这两个包时，MyEclipse会报错： 
 * Access restriction: The type JPEGImageEncoder is not accessible due to  
restriction on required library C:\Java\jre1.6.0_07\lib\rt.jar 
 * 解决方法 
 * Eclipse默认把这些受访问限制的API设成了ERROR。 
 * 只要把Windows-Preferences-Java-Complicer-Errors/Warnings里面的 
 * Deprecated and restricted API中的Forbidden references(access rules)选为Warning就可以编译通过。 
 */  
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.imageio.ImageIO;

public class ImageTest {  
  
    public ImageTest() {}  
  
    public static void main(String[] args) {  
        try {  
            ImageTest test = new ImageTest();  
            test.ImageTset();  
        } catch (Exception e) {  
            System.out.print(e);  
        }  
    }  
  
    public void ImageTset() throws Exception {  
        //创建四个文件对象（注：这里四张图片的宽度都是相同的，因此下文定义BufferedImage宽度就取第一只的宽度就行了）  
        File _file1 = new File("D:/5.jpg");
        File _file2 = new File("D:/2.jpg");
        File _file3 = new File("D:/3.jpg");
        File _file4 = new File("D:/4.jpg");
  
        Image src1 = ImageIO.read(_file1);
        Image src2 = ImageIO.read(_file2);
        Image src3 = ImageIO.read(_file3);
        Image src4 = ImageIO.read(_file4);
          
        //获取图片的宽度  
//        int width = src1.getWidth(null);
        //获取各个图像的高度  
        int height1 = src1.getHeight(null);
        int width1 = src1.getWidth(null);
        int height2 = src2.getHeight(null);
        int width2 = src2.getWidth(null);
        int height3 = src3.getHeight(null);  
        int width3 = src3.getWidth(null);
        int height4 = src4.getHeight(null);
        int width4 = src4.getWidth(null);

        int width = width1 >= width2 ? width1 : width2;
        width = width >= width3 ? width : width3;
        width = width >= width4 ? width : width4;


        int height = height1 >= height2 ? height1 : height2;
        height = height >= height3 ? height : height3;
        height = height >= height4 ? height : height4;
        //构造一个类型为预定义图像类型之一的 BufferedImage。 宽度为第一只的宽度，高度为各个图片高度之和
        BufferedImage tag = new BufferedImage(width * 2 , height * 2, BufferedImage.TYPE_INT_RGB);
        //创建输出流  
        FileOutputStream out = new FileOutputStream("D:/treasureMap.jpg");
        //绘制合成图像  
        Graphics g = tag.createGraphics();

        g.drawImage(src1, width - width1, height - height1, width1, height1, null);
        g.drawImage(src2, width, height - height2, width2, height2, null);
        g.drawImage(src3, width - width3, height, width3, height3, null);
        g.drawImage(src4, width, height, width4, height4, null);

        // 释放此图形的上下文以及它使用的所有系统资源。  
        g.dispose();  
        //将绘制的图像生成至输出流  
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
        encoder.encode(tag);  
        //关闭输出流  
        out.close();  
        System.out.println("藏宝图出来了");  
    }  
}  